package com.kehao.leetcode.basic.string;

import org.junit.Test;

import java.util.ArrayList;

public class StringSolution8 {
    public String countAndSay(int n) {
        ArrayList<Character> beforeCharList = new ArrayList<>();
        beforeCharList.add('1');
        ArrayList<Character> afterCharList = new ArrayList<>();

        for (int j = 0; j < n-1; j++) {
            int size = 1;
            for (int i = 1; i < beforeCharList.size(); i++) {
                if(beforeCharList.get(i)==beforeCharList.get(i-1)){
                    size++;
                }else{
                    afterCharList.add((char)('0'+size));
                    afterCharList.add(beforeCharList.get(i-1));
                    size = 1;
                }
            }
            if(size!=0){
                afterCharList.add((char)('0'+size));
                afterCharList.add(beforeCharList.get(beforeCharList.size()-1));
            }else {
                afterCharList.add((char)('1'));
                afterCharList.add(beforeCharList.get(beforeCharList.size()-1));
            }

            beforeCharList.clear();
            ArrayList<Character> tmp = beforeCharList;
            beforeCharList = afterCharList;
            afterCharList = tmp;

        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < beforeCharList.size(); i++) {
            sb.append(beforeCharList.get(i));
        }
        return sb.toString() ;
    }

    @Test
    public void test01(){
        System.out.println(countAndSay(5));
    }
}
